#!/bin/bash

# usage
#    ./update-from.sh <copy.of.trunk.dir>

# This tool is useful for copying open changes across machines having
# different subversion client versions, when doing this is more
# practical than upgrading the older subversion client on the target
# machine.

if [ -n "$1" ]&&[ -d "$1" ]&&[ -d "${1}/src/ore" ]
then
 srcd="${1}/src/ore"
 # svn deletes
 for tgtf in $(find src/ore -type f | egrep -v '/\.svn/')
 do
  srcf=$(echo ${tgtf} | sed "s%src/ore/%${srcd}/%")
  if [ ! -f ${srcf} ]
  then
   if ! svn delete ${tgtf}
   then
    echo "Error in 'svn delete ${tgtf}'"
    exit 1
   fi
  fi
 done
 # svn adds
 for srcf in $(find ${srcd} -type f | egrep -v '/\.svn/')
 do
  tgtf=$(echo ${srcf} | sed "s%${srcd}/%src/ore/%")
  if [ ! -f ${tgtf} ]
  then
   tgtd=$(dirname ${tgtf})
   if [ ! -d ${tgtd} ]
   then
    if ! mkdir -p ${tgtd}
    then
     echo "Error in 'mkdir -p ${tgtd}'"
     exit 1
    fi
    if ! svn add ${tgtd}
    then
     echo "Error in 'svn add ${tgtd}'"
     exit 1
    fi
   fi
   if ! cp -p ${srcf} ${tgtf}
   then
    echo "Error in 'cp -p ${srcf} ${tgtf}'"
    exit 1
   fi
   if ! svn add ${tgtf}
   then
    echo "Error in 'svn add ${tgtf}'"
    exit 1
   fi
  fi
 done
 # svn updates
 for srcf in $(find ${srcd} -type f | egrep -v '/\.svn/')
 do
  tgtf=$(echo ${srcf} | sed "s%${srcd}/%src/ore/%")
  if [ -n "$(2>/dev/null diff ${srcf} ${tgtf})" ]
  then
   if ! cp -p ${srcf} ${tgtf}
   then
    echo "Error in 'cp -p ${srcf} ${tgtf}'"
    exit 1
   fi
  fi
 done
 # top level
 srcd=${1}
 for srco in $(ls -d ${srcd}/* )
 do
  if [ -f ${srco} ]
  then
   srcf=${srco}
   tgtf=$(basename $srcf)
   if [ -f ${tgtf} ]
   then
    if ! cp -p ${srcf} ${tgtf}
    then
     echo "Error in 'cp -p ${srcf} ${tgtf}'"
     exit 1
    fi
   else 
    if ! cp -p ${srcf} ${tgtf}
    then
     echo "Error in 'cp -p ${srcf} ${tgtf}'"
     exit 1
    fi
    if ! svn add ${tgtf}
    then
     echo "Error in 'svn add ${tgtf}'"
     exit 1
    fi
   fi
  fi
 done
else
 cat<<EOF>&2
Usage 
  $0 <src.dir>

Description

 The 'src.dir' contains 'src/ore'.

 This tool is useful for copying open changes across machines having
 different subversion client versions, when doing this is more
 practical than upgrading the older subversion client on the target
 machine.

EOF
 exit 1
fi
